package com.seari.controller;

import com.seari.bean.DataGridBean;
import com.seari.pojo.FlowLinkDeploy;
import com.seari.pojo.FlowMajor;
import com.seari.service.DeployDataService;
import com.seari.service.FaultService;
import com.seari.service.FlowMajorService;
import com.seari.utils.DateUtil;
import com.seari.utils.StringHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
@RequestMapping("FlowMajor/")
public class FlowMajorController {
    @Autowired
    private FlowMajorService flowMajorService;

    @Autowired
    private FaultService faultService;

    @Autowired
    private DeployDataService deployDataService;

    /**
     * 专业流程
     * @param faultId
     * @return
     */
    @RequestMapping("getFlowMajorStatu")
    @ResponseBody
    public DataGridBean<FlowMajor> getFlowMajorStatu(String faultId,String tag) {
        DataGridBean<FlowMajor> dataGridBean = new DataGridBean<>();
        if (faultId == null||faultId.equals("")) {
            dataGridBean.setCode(StringHelper.MESSAGE_TYPE_ERROR);
            dataGridBean.setMsg("参数为空...");
            return dataGridBean;
        }
        List<FlowMajor> flowMajorList = flowMajorService.getFlowMajorStatu(faultId,tag);
        for (FlowMajor flowMajor : flowMajorList) {
            String firstTime = flowMajor.getDisposeTime();
            String secTime = flowMajor.getAnswerTime();
            if (firstTime == null||firstTime.equals("")||secTime==null||secTime.equals("")) {
                flowMajor.setSubTime("--");
            }else{
                long subTime = DateUtil.timeSub(secTime,firstTime)/60;
                String timeStr = DateUtil.minToHoursToDays(subTime);
                flowMajor.setSubTime(timeStr);
            }
        }
        dataGridBean.setCode(StringHelper.MESSAGE_TYPE_SUCCESS);
        dataGridBean.setTotal(flowMajorList.size());
        dataGridBean.setRows(flowMajorList);
        return dataGridBean;
    }

    /**
     * 总流程
     * @return
     */
    @RequestMapping("getFlowMajorAll")
    @ResponseBody
    public DataGridBean<FlowMajor> getFlowMajorAll(String faultId) {
        DataGridBean<FlowMajor> dataGridBean = new DataGridBean<>();

        if (faultId == null || faultId.equals("")) {
            dataGridBean.setCode(StringHelper.MESSAGE_TYPE_ERROR);
            dataGridBean.setMsg("参数为空...");
            return dataGridBean;
        }
        FlowMajor flowMajor = new FlowMajor();
        flowMajor.setFaultId(faultId);
        List<FlowMajor> flowMajorAllList = new ArrayList();
        List<Integer> majorList = flowMajorService.getFlowAllMajor(faultId);
        List<FlowMajor> FlowMajorOrderDis = flowMajorService.getFlowMajorOrderDis(flowMajor);
        flowMajorAllList.addAll(FlowMajorOrderDis);
        List<FlowMajor> flowMajorOrderCurrent = flowMajorService.getFlowMajorOrderCurrent(flowMajor);
        flowMajorAllList.addAll(flowMajorOrderCurrent);
        List<FlowMajor> FlowMajorOrderAns = flowMajorService.getFlowMajorOrderAns(flowMajor);
        flowMajorAllList.addAll(FlowMajorOrderAns);


        String flowCount = flowMajorService.getFlowCount(faultId);
        dataGridBean.setFlowCount(flowCount);
        dataGridBean.setMajorList(majorList);
        for (FlowMajor flowMajor1 : flowMajorAllList) {
            String firstTime = flowMajor1.getDisposeTime();
            String secTime = flowMajor1.getAnswerTime();
            if (firstTime == null||firstTime.equals("")||secTime==null||secTime.equals("")) {
                flowMajor1.setSubTime("--");
            }else{
                long subTime = DateUtil.timeSub(secTime,firstTime)/60;
                String timeStr = DateUtil.minToHoursToDays(subTime);
                flowMajor1.setSubTime(timeStr);
            }
        }
        dataGridBean.setDiffList(getDisAnsTimeDiff(faultId));
        dataGridBean.setCode(StringHelper.MESSAGE_TYPE_SUCCESS);
        dataGridBean.setTotal(flowMajorAllList.size());
        dataGridBean.setRows(flowMajorAllList);
        return dataGridBean;
    }

    @RequestMapping("updDisposeTimeAndStatu")
    @ResponseBody
    public DataGridBean updDisposeTimeAndStatu(FlowMajor flowMajor){
        DataGridBean dataGridBean = new DataGridBean<>();
        String now = DateUtil.getNow();
        flowMajor.setDisposeTime(now);
        int count1 = flowMajorService.updDisposeTimeAndStatu(flowMajor);
//        int count = flowMajorService.updDisposeTimeAndStatuZ(flowMajor);
        if(count1 < 1){
            dataGridBean.setCode(StringHelper.MESSAGE_TYPE_ERROR);
            dataGridBean.setMsg("更新条数为0...");
            return dataGridBean;
        }else{
            dataGridBean.setCode(StringHelper.MESSAGE_TYPE_SUCCESS);
            dataGridBean.setFaultStatu("0");
            dataGridBean.setMsg("更新成功...");
        }
        Integer tag = flowMajor.getTag();
        String faultId = flowMajor.getFaultId();
        Integer flowLinkId = flowMajor.getFlowLinkId();
        if (tag == null||faultId==null||flowLinkId==null) {
            dataGridBean.setCode(StringHelper.MESSAGE_TYPE_ERROR);
            dataGridBean.setMsg("参数空...");
            return dataGridBean;
        }

        List<Integer> majorList = flowMajorService.getFlowAllMajor(faultId);
        dataGridBean.setMajorList(majorList);
        if(tag!=1){
            List<FlowLinkDeploy> deployData = deployDataService.getDeployData(tag);
            for (int i = 0; i<deployData.size();i++){
                FlowLinkDeploy flowLinkDeploy = deployData.get(i);
                Integer flowLinkId1 = flowLinkDeploy.getFlowLinkId();
                System.out.println("currentNodeLinkID>>>>"+flowLinkId1);
                if(flowLinkId==flowLinkId1){
                    FlowMajor flowMajor1 = new FlowMajor();
                    Integer nextNode = flowLinkDeploy.getNextNode();
                    flowMajor1.setFlowLinkId(nextNode);
                    flowMajor1.setFaultId(faultId);
                    flowMajor1.setTag(tag);
                    if(flowMajor1.getFlowLinkId()>8&&flowMajor1.getFlowLinkId()<14){
                        flowMajor1.setFlowLinkId(tag);
                    }
                    int countFlow = flowMajorService.updCurrentNodeStatu(flowMajor1);
                }
            }
            String answerTime = flowMajor.getAnswerTime();
            long subTime = DateUtil.timeSub(answerTime,now)/60;
            String timeStr = DateUtil.minToHoursToDays(subTime);
            String forDisAnsDiffFirst = DateUtil.DateDiffMin(subTime,answerTime);
            for (int j = flowLinkId+1; j < 16; j++){
//                if(j!=5){
                    if(j>8&&j<14){
                        flowMajor.setFlowLinkId(tag);
                        j=13;
                    }else{
                        flowMajor.setFlowLinkId(j);
                    }
                    String ansTimeByLinkIdForDiffTime = flowMajorService.getAnsTimeByLinkIdForDiffTime(flowMajor);
                    String forDisAnsDiff = DateUtil.DateDiffMin(subTime,ansTimeByLinkIdForDiffTime);
                    flowMajor.setAnswerTime(forDisAnsDiff);
                    int i1 = flowMajorService.updAnsTimeForDisAnsDiff(flowMajor);
//                }
            }
            Map<String,String> currentDisposeTimeMap = new HashMap<String,String>();
            currentDisposeTimeMap.put("currentDisposeTime",now);
            currentDisposeTimeMap.put("disAnsDiff",timeStr);
            List<Map<String,String>> currentDisposeTimeList = new ArrayList<>();
            currentDisposeTimeMap.put("currentAnswerTime",forDisAnsDiffFirst);
            currentDisposeTimeList.add(currentDisposeTimeMap);
            dataGridBean.setCurrentDisAnsTime(currentDisposeTimeList);
        }

        if(flowLinkId >= 14){
            int i = flowMajorService.selFlowIsMajor(faultId);
            if(i==0){
                //执行更新fault中statu
                int count2 = faultService.updFaultStatu(flowMajor.getFaultId());
                if(count2<1) {
                    dataGridBean.setCode(StringHelper.MESSAGE_TYPE_ERROR);
                    dataGridBean.setFaultStatu("-1");
                    dataGridBean.setMsg("更新出错...");
                }else if(count2 >= 0){
                    dataGridBean.setCode(StringHelper.MESSAGE_TYPE_SUCCESS);
                    dataGridBean.setFaultStatu("1");
                    dataGridBean.setMsg("faultStatus");
                }
            }
        }
        return dataGridBean;
    }

    public List<Map<String,Object>> getDisAnsTimeDiff(String faultId) {
        Map<String,Object> diffMap = new HashMap<>();
        Integer satuEnd = null;
        Object ansStr = 0;
        Object disStr = 0;
        long disStatu = -1;
        long ansStatu = -1;
        Integer ansMaxLinkId = flowMajorService.getAnswerMaxLinkId(faultId);
        if(ansMaxLinkId !=null){
            if(ansMaxLinkId >= 6){
                String ansDiff = flowMajorService.getAnswerTimeDiff(faultId);
                if(ansDiff!=null){
                    Integer ans = Integer.valueOf(ansDiff);
                    ansStr = DateUtil.minToHoursToDays(ans);
                }
                ansStatu = 1;
            }else {
                String ansTime = flowMajorService.getDisposeTime(faultId);
                ansStr = DateUtil.stringtoDate(ansTime, DateUtil.FORMAT_ONE).getTime();
                ansStatu = 0;
            }
        }else {
            satuEnd = flowMajorService.getflowIsExit(faultId);
            if(satuEnd!=null){
                if(satuEnd == 1){
                    String ansDiff = flowMajorService.getAnswerTimeDiff(faultId);
                    if(ansDiff!=null){
                        Integer ans = Integer.valueOf(ansDiff);
                        ansStr = DateUtil.minToHoursToDays(ans);
                    }
                    ansStatu = 1;
                }
            }
        }

        Integer disMinLinkId = flowMajorService.getDisposeMinLinkId(faultId);

        if(disMinLinkId !=null){
            if(disMinLinkId > 14){
                String disDiff = flowMajorService.getDisposeTimeDiff(faultId);
                if(disDiff!=null){
                    Integer dis = Integer.valueOf(disDiff);
                    disStr = DateUtil.minToHoursToDays(dis);
                }
                disStatu = 1;
            }else {
                String disposeTime = flowMajorService.getDisposeTime(faultId);
                disStr = DateUtil.stringtoDate(disposeTime, DateUtil.FORMAT_ONE).getTime();
                disStatu = 0;
            }
        }else {
            satuEnd = flowMajorService.getflowIsExit(faultId);
            if(satuEnd!=null){
                if(satuEnd == 1){
                    String disDiff = flowMajorService.getDisposeTimeDiff(faultId);
                    if(disDiff!=null){
                        Integer dis = Integer.valueOf(disDiff);
                        disStr = DateUtil.minToHoursToDays(dis);
                    }
                    disStatu = 1;
                }
            }
        }
        diffMap.put("ansDiff",ansStr);
        diffMap.put("disDiff",disStr);
        diffMap.put("ansStatu",ansStatu);
        diffMap.put("disStatu",disStatu);
        List list = new ArrayList();
        list.add(diffMap);
        return list;
    }

































    /**
     * 总流程
     * @return
     */
    @RequestMapping("getFlowMajorAlls")
    @ResponseBody
    public DataGridBean<FlowMajor> getFlowMajorAlls(String faultId) {
        DataGridBean<FlowMajor> dataGridBean = new DataGridBean<>();

        if (faultId == null || faultId.equals("")) {
            dataGridBean.setCode(StringHelper.MESSAGE_TYPE_ERROR);
            dataGridBean.setMsg("参数为空...");
            return dataGridBean;
        }
        FlowMajor flowMajor = new FlowMajor();
        flowMajor.setFaultId(faultId);
        List<FlowMajor> flowMajorAllList = new ArrayList();
        List<Integer> majorList = flowMajorService.getFlowAllMajor(faultId);
        List<FlowMajor> FlowMajorOrderDis = flowMajorService.getFlowMajorOrderDis(flowMajor);
        flowMajorAllList.addAll(FlowMajorOrderDis);
        List<FlowMajor> flowMajorOrderCurrent = flowMajorService.getFlowMajorOrderCurrent(flowMajor);
        flowMajorAllList.addAll(flowMajorOrderCurrent);
        List<FlowMajor> FlowMajorOrderAns = flowMajorService.getFlowMajorOrderAns(flowMajor);
        flowMajorAllList.addAll(FlowMajorOrderAns);


        String flowCount = flowMajorService.getFlowCount(faultId);
        dataGridBean.setFlowCount(flowCount);
        dataGridBean.setMajorList(majorList);
        for (FlowMajor flowMajor1 : flowMajorAllList) {
            String firstTime = flowMajor1.getDisposeTime();
            String secTime = flowMajor1.getAnswerTime();
            if (firstTime == null||firstTime.equals("")||secTime==null||secTime.equals("")) {
                flowMajor1.setSubTime("--");
            }else{
                long subTime = DateUtil.timeSub(secTime,firstTime)/60;
                String timeStr = DateUtil.minToHoursToDays(subTime);
                flowMajor1.setSubTime(timeStr);
            }
        }
        dataGridBean.setDiffList(getDisAnsTimeDiffs(faultId));
        dataGridBean.setCode(StringHelper.MESSAGE_TYPE_SUCCESS);
        dataGridBean.setTotal(flowMajorAllList.size());
        dataGridBean.setRows(flowMajorAllList);
        return dataGridBean;
    }


    public List<Map<String,Object>> getDisAnsTimeDiffs(String faultId) {
        Map<String,Object> diffMap = new HashMap<>();
        Integer satuEnd = null;
        Object ansStr = 0;
        Object disStr = 0;
        long disStatu = -1;
        long ansStatu = -1;
        Integer ansMaxLinkId = flowMajorService.getAnswerMaxLinkId(faultId);
        if(ansMaxLinkId !=null){
            if(ansMaxLinkId >= 6){
                String ansDiff = flowMajorService.getAnswerTimeDiff(faultId);
                if(ansDiff!=null){
                    Integer ans = Integer.valueOf(ansDiff);
                    ansStr = DateUtil.minToHToDToS(ans);
                }
                ansStatu = 1;
            }else {
                String ansTime = flowMajorService.getDisposeTime(faultId);
                ansStr = DateUtil.stringtoDate(ansTime, DateUtil.FORMAT_ONE).getTime();
                ansStatu = 0;
            }
        }else {
            satuEnd = flowMajorService.getflowIsExit(faultId);
            if(satuEnd!=null){
                if(satuEnd == 1){
                    String ansDiff = flowMajorService.getAnswerTimeDiff(faultId);
                    if(ansDiff!=null){
                        Integer ans = Integer.valueOf(ansDiff);
                        ansStr = DateUtil.minToHToDToS(ans);
                    }
                    ansStatu = 1;
                }
            }
        }

        Integer disMinLinkId = flowMajorService.getDisposeMinLinkId(faultId);

        if(disMinLinkId !=null){
            if(disMinLinkId > 14){
                String disDiff = flowMajorService.getDisposeTimeDiff(faultId);
                if(disDiff!=null){
                    Integer dis = Integer.valueOf(disDiff);
                    disStr = DateUtil.minToHToDToS(dis);
                }
                disStatu = 1;
            }else {
                String disposeTime = flowMajorService.getDisposeTime(faultId);
                disStr = DateUtil.stringtoDate(disposeTime, DateUtil.FORMAT_ONE).getTime();
                disStatu = 0;
            }
        }else {
            satuEnd = flowMajorService.getflowIsExit(faultId);
            if(satuEnd!=null){
                if(satuEnd == 1){
                    String disDiff = flowMajorService.getDisposeTimeDiff(faultId);
                    if(disDiff!=null){
                        Integer dis = Integer.valueOf(disDiff);
                        disStr = DateUtil.minToHToDToS(dis);
                    }
                    disStatu = 1;
                }
            }
        }
        diffMap.put("ansDiff",ansStr);
        diffMap.put("disDiff",disStr);
        diffMap.put("ansStatu",ansStatu);
        diffMap.put("disStatu",disStatu);
        List list = new ArrayList();
        list.add(diffMap);
        return list;
    }

















}